package com.ssy.lingxi.pay.controller.feign;

import com.ssy.lingxi.common.exception.BusinessException;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.pay.api.common.CcbDigitalPayTypeEnum;
import com.ssy.lingxi.pay.api.model.vo.request.ccb.*;
import com.ssy.lingxi.pay.service.ICcbPayService;
import com.ssy.lingxi.pay.service.IRefundService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @Description: 建行支付内部接口
 * @Author: HuangJieZhou
 * @Date: 2021/12/20 15:46
 * @Version: 2.0.0
 **/
@RestController
@RequestMapping("/pay/ccb")
public class CcbPayFeignController {

    @Resource
    private ICcbPayService iCcbPayService;

    @Resource
    private IRefundService refundService;

    /**
     * 建行数字人民币支付
     * @param ccbDigitalPayVo 接口请求参数
     * @return payType支付类型-1:返回二维码路径; 2-返回h5页面
     */
    @RequestMapping(value = "/digitalPay", method = RequestMethod.POST)
    public Wrapper<String> digitalPay(@RequestBody @Valid CcbDigitalPayVo ccbDigitalPayVo) {
        if(CcbDigitalPayTypeEnum.WEB.getCode().equals(ccbDigitalPayVo.getPayType())){
            return Wrapper.success(iCcbPayService.digitalPcPay(ccbDigitalPayVo));
        }else if(CcbDigitalPayTypeEnum.APP.getCode().equals(ccbDigitalPayVo.getPayType())){
            return Wrapper.success(iCcbPayService.digitalAppPay(ccbDigitalPayVo));
        }else{
            throw new BusinessException(ResponseCode.PAY_CCB_TRADE_TYPE_NOT_EXIST);
        }
    }

    /**
     * 建行数字人民币支付结果查询
     * @param ccbDigitalPayQueryVo 接口请求参数
     * @return 支付结果，枚举类：CcbDigitalPayResultEnum
     */
    @RequestMapping(value = "/getDigitalPayResult", method = RequestMethod.POST)
    public Wrapper<String> getDigitalPayResult(@RequestBody @Valid CcbDigitalPayQueryVo ccbDigitalPayQueryVo) {
        return Wrapper.success(iCcbPayService.getDigitalPayResult(ccbDigitalPayQueryVo));
    }

    /**
     * 建行数字人民币退款
     * @param digitalRefundVO 接口请求参数
     * @return 成功返回true, 失败直接抛异常
     */
    @RequestMapping(value = "/digitalRefund", method = RequestMethod.POST)
    public Wrapper<Boolean> digitalRefund(@RequestBody @Valid DigitalRefundVO digitalRefundVO) {
        return Wrapper.success(iCcbPayService.digitalRefund(digitalRefundVO));
    }

    /**
     * 建行B2B普通商户支付
     * @param ccbPayVo 接口请求参数
     * @return 跳转建行html页面
     */
    @RequestMapping(value = "/b2bPay", method = RequestMethod.POST)
    public Wrapper<String> b2bPay(@RequestBody @Valid CcbB2bPayVo ccbPayVo) {
        boolean flag = iCcbPayService.payOrderExist(ccbPayVo.getOrderCode());
        if(!flag){
            return Wrapper.success(iCcbPayService.b2bPay(ccbPayVo));
        }else{
            return Wrapper.fail(ResponseCode.PAY_CCB_TRADE_CODE_EXIST);
        }
    }

    /**
     * 建行B2B订单支付状态查询
     * @param ccbAccountQueryVo 请求参数
     * @return 订单状态，枚举类型: CcbB2bPayResultEnum
     */
    @RequestMapping(value = "/b2bPayStatusQuery", method = RequestMethod.POST)
    public Wrapper<String> b2bPayStatusQuery(@RequestBody @Valid CcbB2bPayQueryVo ccbAccountQueryVo) {
        return Wrapper.success(iCcbPayService.b2bPayStatusQuery(ccbAccountQueryVo));
    }

    /**
     * 建行B2B商户退款
     * @param refundVO 请求参数
     * @return 成功返回true, 失败直接抛异常
     */
    @RequestMapping(value = "/b2bRefund", method = RequestMethod.POST)
    public Wrapper<Boolean> b2bRefund(@RequestBody @Valid B2bRefundVO refundVO) {
        return Wrapper.success(refundService.refund(refundVO));
    }

    /**
     * 建行B2B退款查询
     * @param refundVO 请求参数
     * @return 订单状态，枚举类型: CcbB2bPayRefundEnum
     */
    @RequestMapping(value = "/b2bRefundQuery", method = RequestMethod.POST)
    public Wrapper<String> b2bRefundQuery(@RequestBody @Valid B2bRefundQueryVO refundVO) {
        return Wrapper.success(refundService.refundQuery(refundVO));
    }
}
